package io.gitee.loulan_yxq.owner.http;

import io.gitee.loulan_yxq.owner.core.exception.IORuntimeException;
import io.gitee.loulan_yxq.owner.core.io.IoTool;
import io.gitee.loulan_yxq.owner.core.tool.CharsetTool;
import io.gitee.loulan_yxq.owner.core.tool.ObjectTool;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;

/*********************************************************
 ** 请求响应对象
 ** <br><br>
 ** Date: Created in 2021/11/1  18:12
 ** @author loulan
 ** @version 0.0.0
 *********************************************************/
public class HttpResponse {
    private CloseableHttpResponse httpResponse;
    private CloseableHttpClient httpClient;
    private Charset charset = CharsetTool.defaultCharset();

    private HttpResponse(Builder builder) {
        this.httpResponse = builder.httpResponse;
        this.httpClient = builder.httpClient;
        this.charset = builder.charset;
    }

    public static class Builder {
        private CloseableHttpResponse httpResponse;
        private CloseableHttpClient httpClient;
        private Charset charset = CharsetTool.defaultCharset();

        public Builder httpResponse(CloseableHttpResponse httpResponse) {
            this.httpResponse = httpResponse;
            return this;
        }

        public Builder httpClient(CloseableHttpClient httpClient) {
            this.httpClient = httpClient;
            return this;
        }

        public Builder charset(Charset charset) {
            this.charset = charset;
            return this;
        }

        public HttpResponse build() {
            return new HttpResponse(this);
        }
    }

    /**
     * 设置编码方式
     *
     * @param charset 编码
     * @return {@link HttpResponse}对象
     * @author :loulan
     */
    public HttpResponse charset(Charset charset) {
        this.charset = ObjectTool.isNull(charset) ? CharsetTool.defaultCharset() : charset;
        return this;
    }


    /**
     * 获取响应数据
     *
     * @return 响应数据的字符串
     * @author :loulan
     */
    public String body() {
        try {
            String body = EntityUtils.toString(httpResponse.getEntity(), charset);
            return body;
        } catch (IOException e) {
            throw new IORuntimeException(e);
        } finally {
            close();
        }
    }

    /**
     * 获取请求结果的输入流对象
     *
     * @return 请求结果的输入流
     * @author :loulan
     */
    public InputStream bodyStrem() {
        try {
            InputStream is = httpResponse.getEntity().getContent();
            return is;
        } catch (IOException e) {
            throw new IORuntimeException(e);
        } finally {
            close();
        }
    }

    /**
     * 将请求体数据输出到输出流中
     *
     * @param os 输出流
     * @author :loulan
     */
    public boolean body(OutputStream os) {
        try {
            InputStream is = httpResponse.getEntity().getContent();
            IoTool.copy(is, os);
            IoTool.close(is);
            return true;
        } catch (IOException e) {
            throw new IORuntimeException(e);
        } finally {
            close();
        }
    }

    /**
     * HttpResponse使用完毕需要关闭各种内部的流
     *
     * @author :loulan
     */
    public void close() {
        HttpClientUtils.closeQuietly(httpResponse);
        HttpClientUtils.closeQuietly(httpClient);
    }
}
